<?php
/**
 * 用户定制需求模型
 */

namespace app\badmin\model;


use app\badmin\model\BusinessArea;
use app\badmin\model\HouseVillage;
use app\common\lib\RespCode;
use app\common\model\BaseModel;
use custom\Err;
use think\Db;

class AgentUserDemand extends BaseModel {

    /**
     * 获取单条需求
     * @param $map
     * @param $column
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getOne($map, $column) {
        return db("agent_user_demand")
            ->where($map)
            ->field($column)
            ->order('utime', 'desc')
            ->find();
    }

    /**
     * 需求展示处理
     * @param $params
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function userDemand($params, &$data) {
        $column = ['city_code,area_code,price_section,area_section,type,type_id'];
        $agent_user = Db::name("company_agent_user")
            ->where('id', $params['id'])
            ->field('user_id,agent_id')
            ->find();
        if (empty($agent_user)) {
            Err::add('数据不存在', RespCode::PARAMS_ERR);
            return false;
        }
        $data = db('agent_user_demand')
            ->where('user_id', $agent_user['user_id'])
            ->whereRaw('self_write = 1 or agent_id = ' . $agent_user['agent_id'])
            ->order('utime', 'desc')
            ->limit(1)
            ->find();
        if (empty($data)) {
            $data = [];
        } else {
            $data['city'] = Region::getOne([['city_code', '=', $data['city_code']]], 'name')['name'];
            if (!empty($data['area_code'])) {
                $areas = Region::getDatas([['city_code', 'in', $data['area_code']]], 'name');
                $areas = array_column($areas, 'name');
                $data['area'] = join(',', $areas);
            } else {
                $data['area'] = '不限';
            }

            $price_section1 = explode(',', $data['price_section']);
            foreach ($price_section1 as $k1 => $v1) {
                $price_section2 = explode('-', $v1);
                $price_section3[] = Db::name('interval_config')
                    ->where('min', $price_section2[0])
                    ->where('max', $price_section2[1])
                    ->where('type', 1)
                    ->value('name');
            }
            $data['price_section'] = join(',', $price_section3);

            $area_section1 = explode(',', $data['area_section']);
            foreach ($area_section1 as $k2 => $v2) {
                $area_section2 = explode('-', $v2);
                $area_section3[] = Db::name('interval_config')
                    ->where('min', $area_section2[0])
                    ->where('max', $area_section2[1])
                    ->where('type', 2)
                    ->value('name');
            }
            $data['area_section'] = join(',', $area_section3);

            if ($data['type'] == 1) {
                $data['type'] = '不限小区/商圈';
            }
            if ($data['type'] == 2) {
                $v_name = HouseVillage::getOne([['id', '=', $data['type_id']]], 'name')['name'];
                $data['type'] = "只看'" . $v_name . "'小区";
            }
            if ($data['type'] == 3) {
                $a_name = BusinessArea::getOne([['id', '=', $data['type_id']]], 'name')['name'];
                $data['type'] = "只看'" . $a_name . "'商圈";
            }
            unset($data['city_code']);
            unset($data['area_code']);
            unset($data['type_id']);
            if (!empty($data['door_section'])) $data['door_section'] = explode('-', $data['door_section'])[0] . '居室';
            else $data['door_section'] = '- -';
        }
        return true;
    }

}